﻿
using DualPlaneFR4Paster.Common.Services;
using DualPlaneFR4Paster.Extensions;
using DualPlaneFR4Paster.Models;
using Microsoft.EntityFrameworkCore;
using NLog;
using Prism.Commands;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DualPlaneFR4Paster.ViewModels.Dialogs
{
    public class BottomFloorAxisDialogViewModel : DialogViewModel
    {
        #region 变量
        private static Logger logger = LogManager.GetCurrentClassLogger();
        private IGTSCardService Top_A4;
        private IGTSCardService Bottom_A8;
        private CancellationTokenSource source;
        private CancellationTokenSource source0, source1;
        string partNum = string.Empty;
        private IServoModbusService servoModbus;
        private bool isJogButtonPressing = false, isPrtMoving = false;
        #endregion
        #region 属性
        private ObservableCollection<AxisStatusDisp> axisStatusDisps;
        public ObservableCollection<AxisStatusDisp> AxisStatusDisps
        {
            get { return axisStatusDisps; }
            set { SetProperty(ref axisStatusDisps, value); }
        }
        private ObservableCollection<AxisPointDisp> pointList1;
        public ObservableCollection<AxisPointDisp> PointList1
        {
            get { return pointList1; }
            set { SetProperty(ref pointList1, value); }
        }
        private ObservableCollection<AxisZSafeDisp> pointList2;
        public ObservableCollection<AxisZSafeDisp> PointList2
        {
            get { return pointList2; }
            set { SetProperty(ref pointList2, value); }
        }
        private ObservableCollection<AxisZSafeDisp> pointList3;
        public ObservableCollection<AxisZSafeDisp> PointList3
        {
            get { return pointList3; }
            set { SetProperty(ref pointList3, value); }
        }
        private ObservableCollection<ParamDisp> paramList1;
        public ObservableCollection<ParamDisp> ParamList1
        {
            get { return paramList1; }
            set { SetProperty(ref paramList1, value); }
        }
        private bool isBusy;
        public bool IsBusy
        {
            get { return isBusy; }
            set { SetProperty(ref isBusy, value); }
        }
        private ObservableCollection<bool> eXI;
        public ObservableCollection<bool> EXI
        {
            get { return eXI; }
            set { SetProperty(ref eXI, value); }
        }
        private ObservableCollection<bool> eXO;
        public ObservableCollection<bool> EXO
        {
            get { return eXO; }
            set { SetProperty(ref eXO, value); }
        }
        private double testPoint_X;
        public double TestPoint_X
        {
            get { return testPoint_X; }
            set { SetProperty(ref testPoint_X, value); }
        }
        private double testPoint_Y;
        public double TestPoint_Y
        {
            get { return testPoint_Y; }
            set { SetProperty(ref testPoint_Y, value); }
        }
        private double testPoint_Z1;
        public double TestPoint_Z1
        {
            get { return testPoint_Z1; }
            set { SetProperty(ref testPoint_Z1, value); }
        }
        private double testPoint_Z2;
        public double TestPoint_Z2
        {
            get { return testPoint_Z2; }
            set { SetProperty(ref testPoint_Z2, value); }
        }
        private double testPoint_Z3;
        public double TestPoint_Z3
        {
            get { return testPoint_Z3; }
            set { SetProperty(ref testPoint_Z3, value); }
        }
        private double testPoint_Z4;
        public double TestPoint_Z4
        {
            get { return testPoint_Z4; }
            set { SetProperty(ref testPoint_Z4, value); }
        }
        private double testPoint_R1;
        public double TestPoint_R1
        {
            get { return testPoint_R1; }
            set { SetProperty(ref testPoint_R1, value); }
        }
        private double testPoint_R2;
        public double TestPoint_R2
        {
            get { return testPoint_R2; }
            set { SetProperty(ref testPoint_R2, value); }
        }
        private int suckerSelectedIndex;
        public int SuckerSelectedIndex
        {
            get { return suckerSelectedIndex; }
            set { SetProperty(ref suckerSelectedIndex, value); }
        }
        private double jogDist;
        public double JogDist
        {
            get { return jogDist; }
            set
            {
                if (value > 0 && value <= 10)
                {
                    SetProperty(ref jogDist, value);
                }
                else if (value <= 0)
                {
                    SetProperty(ref jogDist, 0.1);
                }
                else if (value > 10)
                {
                    SetProperty(ref jogDist, 10);
                }
            }
        }
        #endregion
        #region 命令
        private DelegateCommand<object> clearAlarmCommand;
        public DelegateCommand<object> ClearAlarmCommand =>
            clearAlarmCommand ?? (clearAlarmCommand = new DelegateCommand<object>(ExecuteClearAlarmCommand));
        private DelegateCommand<object> servoOnCommand;
        public DelegateCommand<object> ServoOnCommand =>
            servoOnCommand ?? (servoOnCommand = new DelegateCommand<object>(ExecuteServoOnCommand));
        private DelegateCommand<object> jog_MouseDownCommand;
        public DelegateCommand<object> Jog_MouseDownCommand =>
            jog_MouseDownCommand ?? (jog_MouseDownCommand = new DelegateCommand<object>(ExecuteJog_MouseDownCommand));
        private DelegateCommand jog_MouseUpCommand;
        public DelegateCommand Jog_MouseUpCommand =>
            jog_MouseUpCommand ?? (jog_MouseUpCommand = new DelegateCommand(ExecuteJog_MouseUpCommand));
        private DelegateCommand<object> pointList1RowEditEndingCommand;
        public DelegateCommand<object> PointList1RowEditEndingCommand =>
            pointList1RowEditEndingCommand ?? (pointList1RowEditEndingCommand = new DelegateCommand<object>(ExecutePointList1RowEditEndingCommand));
        private DelegateCommand<object> pointList1TeachPosCommand;
        public DelegateCommand<object> PointList1TeachPosCommand =>
            pointList1TeachPosCommand ?? (pointList1TeachPosCommand = new DelegateCommand<object>(ExecutePointList1TeachPosCommand));
        private DelegateCommand<object> pointList1JumpPosCommand;
        public DelegateCommand<object> PointList1JumpPosCommand =>
            pointList1JumpPosCommand ?? (pointList1JumpPosCommand = new DelegateCommand<object>(ExecutePointList1JumpPosCommand));
        private DelegateCommand<object> pointList1GoPosCommand;
        public DelegateCommand<object> PointList1GoPosCommand =>
            pointList1GoPosCommand ?? (pointList1GoPosCommand = new DelegateCommand<object>(ExecutePointList1GoPosCommand));

        private DelegateCommand<object> pointList2RowEditEndingCommand;
        public DelegateCommand<object> PointList2RowEditEndingCommand =>
            pointList2RowEditEndingCommand ?? (pointList2RowEditEndingCommand = new DelegateCommand<object>(ExecutePointList2RowEditEndingCommand));
        private DelegateCommand<object> paramList1RowEditEndingCommand;
        public DelegateCommand<object> ParamList1RowEditEndingCommand =>
            paramList1RowEditEndingCommand ?? (paramList1RowEditEndingCommand = new DelegateCommand<object>(ExecuteParamList1RowEditEndingCommand));
        private DelegateCommand<object> pointList2TeachPosCommand;
        public DelegateCommand<object> PointList2TeachPosCommand =>
            pointList2TeachPosCommand ?? (pointList2TeachPosCommand = new DelegateCommand<object>(ExecutePointList2TeachPosCommand));
        private DelegateCommand<object> pointList2GoPosCommand;
        public DelegateCommand<object> PointList2GoPosCommand =>
            pointList2GoPosCommand ?? (pointList2GoPosCommand = new DelegateCommand<object>(ExecutePointList2GoPosCommand));
        private DelegateCommand<object> iOOutCommand;
        public DelegateCommand<object> IOOutCommand =>
            iOOutCommand ?? (iOOutCommand = new DelegateCommand<object>(ExecuteIOOutCommand));
        private DelegateCommand<AxisZSafeDisp> absPosUpdateCommand;
        public DelegateCommand<AxisZSafeDisp> AbsPosUpdateCommand =>
            absPosUpdateCommand ?? (absPosUpdateCommand = new DelegateCommand<AxisZSafeDisp>(ExecuteAbsPosUpdateCommand));
        private DelegateCommand testPointGetCommand;
        public DelegateCommand TestPointGetCommand =>
            testPointGetCommand ?? (testPointGetCommand = new DelegateCommand(ExecuteTestPointGetCommand));
        private DelegateCommand testPointJumpCommand;
        public DelegateCommand TestPointJumpCommand =>
            testPointJumpCommand ?? (testPointJumpCommand = new DelegateCommand(ExecuteTestPointJumpCommand));
        private DelegateCommand testPointGoCommand;
        public DelegateCommand TestPointGoCommand =>
            testPointGoCommand ?? (testPointGoCommand = new DelegateCommand(ExecuteTestPointGoCommand));
        private DelegateCommand<object> suckerIndexSelectCommand;
        public DelegateCommand<object> SuckerIndexSelectCommand =>
            suckerIndexSelectCommand ?? (suckerIndexSelectCommand = new DelegateCommand<object>(ExecuteSuckerIndexSelectCommand));

        void ExecuteSuckerIndexSelectCommand(object obj)
        {
            SuckerSelectedIndex = int.Parse(obj.ToString());
        }
        async void ExecuteTestPointGoCommand()
        {
            var result = _dialogHostService.MessageBox(MessageType.Confirm, "信息", "确认Go运动到\"测试点\"吗？");
            if (result == ButtonResult.Yes)
            {
                IsBusy = true;
                GTSCard.ServoOn(Bottom_A8.A1);//X
                GTSCard.ServoOn(Bottom_A8.A2);//Y
                GTSCard.ServoOn(Bottom_A8.A4);//Z1
                GTSCard.ServoOn(Bottom_A8.A5);//Z2
                GTSCard.ServoOn(Bottom_A8.A6);//Z3
                GTSCard.ServoOn(Bottom_A8.A7);//Z4
                GTSCard.ServoOn(Top_A4.A3);//R1
                GTSCard.ServoOn(Top_A4.A4);//R2
                GTSCard.AxisPosSet(Bottom_A8.A1, GTSCard.GetEnc(Bottom_A8.A1));
                GTSCard.AxisPosSet(Bottom_A8.A2, GTSCard.GetEnc(Bottom_A8.A2));
                GTSCard.AxisPosSet(Bottom_A8.A4, GTSCard.GetEnc(Bottom_A8.A4));
                GTSCard.AxisPosSet(Bottom_A8.A5, GTSCard.GetEnc(Bottom_A8.A5));
                GTSCard.AxisPosSet(Bottom_A8.A6, GTSCard.GetEnc(Bottom_A8.A6));
                GTSCard.AxisPosSet(Bottom_A8.A7, GTSCard.GetEnc(Bottom_A8.A7));
                GTSCard.AxisPosSet(Top_A4.A3, GTSCard.GetEnc(Top_A4.A3));
                GTSCard.AxisPosSet(Top_A4.A4, GTSCard.GetEnc(Top_A4.A4));
                source = new CancellationTokenSource();
                CancellationToken token = source.Token;
                await Task.Run(() => GoTestPointAction(token), token).ContinueWith(t => { IsBusy = false; });
            }
        }
        async void ExecuteTestPointJumpCommand()
        {
            var result = _dialogHostService.MessageBox(MessageType.Confirm, "信息", "确认Jump运动到\"测试点\"吗？");
            if (result == ButtonResult.Yes)
            {
                IsBusy = true;
                GTSCard.ServoOn(Bottom_A8.A1);//X
                GTSCard.ServoOn(Bottom_A8.A2);//Y
                GTSCard.ServoOn(Bottom_A8.A4);//Z1
                GTSCard.ServoOn(Bottom_A8.A5);//Z2
                GTSCard.ServoOn(Bottom_A8.A6);//Z3
                GTSCard.ServoOn(Bottom_A8.A7);//Z4
                GTSCard.ServoOn(Top_A4.A3);//R1
                GTSCard.ServoOn(Top_A4.A4);//R2
                GTSCard.AxisPosSet(Bottom_A8.A1, GTSCard.GetEnc(Bottom_A8.A1));
                GTSCard.AxisPosSet(Bottom_A8.A2, GTSCard.GetEnc(Bottom_A8.A2));
                GTSCard.AxisPosSet(Bottom_A8.A4, GTSCard.GetEnc(Bottom_A8.A4));
                GTSCard.AxisPosSet(Bottom_A8.A5, GTSCard.GetEnc(Bottom_A8.A5));
                GTSCard.AxisPosSet(Bottom_A8.A6, GTSCard.GetEnc(Bottom_A8.A6));
                GTSCard.AxisPosSet(Bottom_A8.A7, GTSCard.GetEnc(Bottom_A8.A7));
                GTSCard.AxisPosSet(Top_A4.A3, GTSCard.GetEnc(Top_A4.A3));
                GTSCard.AxisPosSet(Top_A4.A4, GTSCard.GetEnc(Top_A4.A4));
                source = new CancellationTokenSource();
                CancellationToken token = source.Token;
                await Task.Run(() => JumpTestPointAction(token), token).ContinueWith(t => { IsBusy = false; });
            }
        }
        void ExecuteTestPointGetCommand()
        {
            var result = _dialogHostService.MessageBox(MessageType.Confirm, "信息", "确认示教当前位置到\"测试点\"吗？");
            if (result == ButtonResult.Yes)
            {
                TestPoint_X = Math.Round(GTSCard.GetEnc(Bottom_A8.A1), 3);
                TestPoint_Y = Math.Round(GTSCard.GetEnc(Bottom_A8.A2), 3);
                TestPoint_Z1 = Math.Round(GTSCard.GetEnc(Bottom_A8.A4), 3);
                TestPoint_Z2 = Math.Round(GTSCard.GetEnc(Bottom_A8.A5), 3);
                TestPoint_Z3 = Math.Round(GTSCard.GetEnc(Bottom_A8.A6), 3);
                TestPoint_Z4 = Math.Round(GTSCard.GetEnc(Bottom_A8.A7), 3);
                TestPoint_R1 = Math.Round(GTSCard.GetEnc(Top_A4.A3), 2);
                TestPoint_R2 = Math.Round(GTSCard.GetEnc(Top_A4.A4), 2);
            }
        }
        void ExecuteAbsPosUpdateCommand(AxisZSafeDisp axisZSafeDisp)
        {
            var result = _dialogHostService.MessageBox(MessageType.Confirm, "确认", $"确定更新\"{axisZSafeDisp.Alias}\"位置吗?");
            if (result == ButtonResult.Yes)
            {
                double pos = 0;
                switch (axisZSafeDisp.Name)
                {
                    case "BottomZ1":
                        {
                            int pls = servoModbus.ReadInovance(13);
                            pos = Math.Round(pls * Bottom_A8.A4.Equiv, 3);
                            GTSCard.AxisPosSet(Bottom_A8.A4, 0);
                            GTSCard.AxisEncSet(Bottom_A8.A4, 0);
                        }
                        break;
                    case "BottomZ2":
                        {
                            int pls = servoModbus.ReadInovance(14);
                            pos = Math.Round(pls * Bottom_A8.A5.Equiv, 3);
                            GTSCard.AxisPosSet(Bottom_A8.A5, 0);
                            GTSCard.AxisEncSet(Bottom_A8.A5, 0);
                        }
                        break;
                    case "BottomZ3":
                        {
                            int pls = servoModbus.ReadInovance(15);
                            pos = Math.Round(pls * Bottom_A8.A6.Equiv, 3);
                            GTSCard.AxisPosSet(Bottom_A8.A6, 0);
                            GTSCard.AxisEncSet(Bottom_A8.A6, 0);
                        }
                        break;
                    case "BottomZ4":
                        {
                            int pls = servoModbus.ReadInovance(16);
                            pos = Math.Round(pls * Bottom_A8.A7.Equiv, 3);
                            GTSCard.AxisPosSet(Bottom_A8.A7, 0);
                            GTSCard.AxisEncSet(Bottom_A8.A7, 0);
                        }
                        break;
                    case "BottomR1":
                        {
                            int pls = servoModbus.ReadInovance(17);
                            pos = Math.Round(pls * Top_A4.A3.Equiv, 2);
                            GTSCard.AxisPosSet(Top_A4.A3, 0);
                            GTSCard.AxisEncSet(Top_A4.A3, 0);
                        }
                        break;
                    case "BottomR2":
                        {
                            int pls = servoModbus.ReadInovance(18);
                            pos = Math.Round(pls * Top_A4.A4.Equiv, 2);
                            GTSCard.AxisPosSet(Top_A4.A4, 0);
                            GTSCard.AxisEncSet(Top_A4.A4, 0);
                        }
                        break;
                    default:
                        break;
                }

                using (var sysDb = new SysContext())
                {
                    var param = sysDb.MSystemParams.FirstOrDefault(p => p.Id == axisZSafeDisp.Id);
                    if (param != null)
                    {
                        param.Value = pos.ToString();
                        sysDb.SaveChanges();
                    }
                }
                axisZSafeDisp.ZPos = pos;
            }
        }
        void ExecuteIOOutCommand(object obj)
        {
            Debug.WriteLine(EXO[int.Parse(obj.ToString())]);
            GTSCard.SetDo(Bottom_A8.A1.CardNo, (short)int.Parse(obj.ToString()), (short)(EXO[int.Parse(obj.ToString())] ? 0 : 1));
        }
        async void ExecutePointList2GoPosCommand(object obj)
        {
            if (obj != null)
            {
                var vpoint = (AxisZSafeDisp)obj;
                var result = _dialogHostService.MessageBox(MessageType.Confirm, "信息", $"确认运动到\n\"{vpoint.Alias}\"吗？");
                if (result == ButtonResult.Yes)
                {
                    IsBusy = true;
                    GTSCard.ServoOn(Bottom_A8.A4);//Z1
                    GTSCard.ServoOn(Bottom_A8.A5);//Z2
                    GTSCard.ServoOn(Bottom_A8.A6);//Z3
                    GTSCard.ServoOn(Bottom_A8.A7);//Z4
                    GTSCard.AxisPosSet(Bottom_A8.A4, GTSCard.GetEnc(Bottom_A8.A4));
                    GTSCard.AxisPosSet(Bottom_A8.A5, GTSCard.GetEnc(Bottom_A8.A5));
                    GTSCard.AxisPosSet(Bottom_A8.A6, GTSCard.GetEnc(Bottom_A8.A6));
                    GTSCard.AxisPosSet(Bottom_A8.A7, GTSCard.GetEnc(Bottom_A8.A7));

                    source = new CancellationTokenSource();
                    CancellationToken token = source.Token;
                    await Task.Run(() => GoAction(token, vpoint), token).ContinueWith(t => { IsBusy = false; });
                }
            }
            else
            {
                _dialogHostService.MessageBox(MessageType.Message, "信息", "未选择任何项目");
            }
        }
        void ExecutePointList2TeachPosCommand(object obj)
        {
            if (obj != null)
            {
                var vpoint = (AxisZSafeDisp)obj;
                var result = _dialogHostService.MessageBox(MessageType.Confirm, "信息", $"确认示教\"{vpoint.Alias}\"吗？");
                if (result == ButtonResult.Yes)
                {
                    switch (vpoint.Name)
                    {
                        case "Z1SafePos":
                            vpoint.ZPos = Math.Round(GTSCard.GetEnc(Bottom_A8.A4), 3);
                            break;
                        case "Z2SafePos":
                            vpoint.ZPos = Math.Round(GTSCard.GetEnc(Bottom_A8.A5), 3);
                            break;
                        case "Z3SafePos":
                            vpoint.ZPos = Math.Round(GTSCard.GetEnc(Bottom_A8.A6), 3);
                            break;
                        default:
                            vpoint.ZPos = Math.Round(GTSCard.GetEnc(Bottom_A8.A7), 3);
                            break;
                    }
                    using (var db = new DTContext(partNum))
                    {
                        var point = db.MParams.FirstOrDefault(p => p.Id == vpoint.Id && p.Floor == 2);
                        if (point != null)
                        {
                            logger.Info($"{MLogType.调试}:点坐标\"{vpoint.Alias}\"({point.Value})→({vpoint.ZPos})");
                            point.Value = vpoint.ZPos.ToString();
                            db.SaveChanges();
                        }
                    }
                }
            }
            else
            {
                _dialogHostService.MessageBox(MessageType.Message, "信息", "未选择任何项目");
            }
        }
        void ExecuteParamList1RowEditEndingCommand(object obj)
        {
            if (obj != null)
            {
                var vparam = (ParamDisp)obj;
                using (var db = new DTContext(partNum))
                {
                    var param = db.MParams.FirstOrDefault(p => p.Id == vparam.Id && p.Floor == 2);
                    if (param != null)
                    {
                        logger.Info($"{MLogType.调试}:参数\"{vparam.Alias}\"({param.Value})→({vparam.Value})");
                        param.Value = vparam.Value?.ToString();
                        db.SaveChanges();
                    }
                }
            }
        }
        void ExecutePointList2RowEditEndingCommand(object obj)
        {
            if (obj != null)
            {
                var vzsf = (AxisZSafeDisp)obj;
                using (var db = new DTContext(partNum))
                {
                    var zsf = db.MParams.FirstOrDefault(p => p.Id == vzsf.Id && p.Floor == 2);
                    if (zsf != null)
                    {
                        logger.Info($"{MLogType.调试}:Z安全位\"{zsf.Alias}\"({zsf.Value})→({vzsf.ZPos})");
                        zsf.Value = vzsf.ZPos.ToString();
                        db.SaveChanges();
                    }
                }
            }
        }
        async void ExecutePointList1GoPosCommand(object obj)
        {
            if (obj != null)
            {
                var vpoint = (AxisPointDisp)obj;
                var result = _dialogHostService.MessageBox(MessageType.Confirm, "信息", $"确认Go运动到\n\"{vpoint.Alias}\"吗？");
                if (result == ButtonResult.Yes)
                {
                    IsBusy = true;
                    GTSCard.ServoOn(Bottom_A8.A1);//X
                    GTSCard.ServoOn(Bottom_A8.A2);//Y
                    GTSCard.ServoOn(Bottom_A8.A4);//Z1
                    GTSCard.ServoOn(Bottom_A8.A5);//Z2
                    GTSCard.ServoOn(Bottom_A8.A6);//Z3
                    GTSCard.ServoOn(Bottom_A8.A7);//Z4
                    GTSCard.ServoOn(Top_A4.A3);//R1
                    GTSCard.ServoOn(Top_A4.A4);//R2
                                               //await Task.Delay(500);
                    GTSCard.AxisPosSet(Bottom_A8.A1, GTSCard.GetEnc(Bottom_A8.A1));
                    GTSCard.AxisPosSet(Bottom_A8.A2, GTSCard.GetEnc(Bottom_A8.A2));
                    GTSCard.AxisPosSet(Bottom_A8.A4, GTSCard.GetEnc(Bottom_A8.A4));
                    GTSCard.AxisPosSet(Bottom_A8.A5, GTSCard.GetEnc(Bottom_A8.A5));
                    GTSCard.AxisPosSet(Bottom_A8.A6, GTSCard.GetEnc(Bottom_A8.A6));
                    GTSCard.AxisPosSet(Bottom_A8.A7, GTSCard.GetEnc(Bottom_A8.A7));
                    GTSCard.AxisPosSet(Top_A4.A3, GTSCard.GetEnc(Top_A4.A3));
                    GTSCard.AxisPosSet(Top_A4.A4, GTSCard.GetEnc(Top_A4.A4));
                    source = new CancellationTokenSource();
                    CancellationToken token = source.Token;
                    if (vpoint.Name.Contains("SuckPicker"))
                    {
                        AxisPointDisp _vpoint = vpoint; ;
                        using var db = new DTContext(partNum);
                        switch (SuckerSelectedIndex)
                        {
                            case 1:
                                {
                                    var _p0 = db.XYZRPoints.FirstOrDefault(p => p.Name == "CenterSuck2" && p.Floor == 2);
                                    if (_p0 != null)
                                    {
                                        _vpoint = new AxisPointDisp
                                        {
                                            ZIndex = 1,
                                            RIndex = 0,
                                            X = vpoint.X - _p0.X,
                                            Y = vpoint.Y - _p0.Y,
                                            Z = vpoint.Z,
                                            R = vpoint.R
                                        };
                                    }
                                }
                                break;
                            case 2:
                                {
                                    var _p0 = db.XYZRPoints.FirstOrDefault(p => p.Name == "CenterSuck3" && p.Floor == 2);
                                    if (_p0 != null)
                                    {
                                        _vpoint = new AxisPointDisp
                                        {
                                            ZIndex = 2,
                                            RIndex = 1,
                                            X = vpoint.X - _p0.X,
                                            Y = vpoint.Y - _p0.Y,
                                            Z = vpoint.Z,
                                            R = vpoint.R
                                        };
                                    }
                                }
                                break;
                            case 3:
                                {
                                    var _p0 = db.XYZRPoints.FirstOrDefault(p => p.Name == "CenterSuck4" && p.Floor == 2);
                                    if (_p0 != null)
                                    {
                                        _vpoint = new AxisPointDisp
                                        {
                                            ZIndex = 3,
                                            RIndex = 1,
                                            X = vpoint.X - _p0.X,
                                            Y = vpoint.Y - _p0.Y,
                                            Z = vpoint.Z,
                                            R = vpoint.R
                                        };
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                        await Task.Run(() => GoXYZRAction(token, _vpoint), token).ContinueWith(t => { IsBusy = false; });
                    }
                    else
                    {
                        await Task.Run(() => GoXYZRAction(token, vpoint), token).ContinueWith(t => { IsBusy = false; });
                    }
                }
            }
            else
            {
                _dialogHostService.MessageBox(MessageType.Message, "信息", "未选择任何项目");
            }
        }
        async void ExecutePointList1JumpPosCommand(object obj)
        {
            if (obj != null)
            {
                var vpoint = (AxisPointDisp)obj;
                var result = _dialogHostService.MessageBox(MessageType.Confirm, "信息", $"确认Jump运动到\n\"{vpoint.Alias}\"吗？");
                if (result == ButtonResult.Yes)
                {
                    IsBusy = true;
                    GTSCard.ServoOn(Bottom_A8.A1);//X
                    GTSCard.ServoOn(Bottom_A8.A2);//Y
                    GTSCard.ServoOn(Bottom_A8.A4);//Z1
                    GTSCard.ServoOn(Bottom_A8.A5);//Z2
                    GTSCard.ServoOn(Bottom_A8.A6);//Z3
                    GTSCard.ServoOn(Bottom_A8.A7);//Z4
                    GTSCard.ServoOn(Top_A4.A3);//R1
                    GTSCard.ServoOn(Top_A4.A4);//R2
                    //await Task.Delay(500);
                    GTSCard.AxisPosSet(Bottom_A8.A1, GTSCard.GetEnc(Bottom_A8.A1));
                    GTSCard.AxisPosSet(Bottom_A8.A2, GTSCard.GetEnc(Bottom_A8.A2));
                    GTSCard.AxisPosSet(Bottom_A8.A4, GTSCard.GetEnc(Bottom_A8.A4));
                    GTSCard.AxisPosSet(Bottom_A8.A5, GTSCard.GetEnc(Bottom_A8.A5));
                    GTSCard.AxisPosSet(Bottom_A8.A6, GTSCard.GetEnc(Bottom_A8.A6));
                    GTSCard.AxisPosSet(Bottom_A8.A7, GTSCard.GetEnc(Bottom_A8.A7));
                    GTSCard.AxisPosSet(Top_A4.A3, GTSCard.GetEnc(Top_A4.A3));
                    GTSCard.AxisPosSet(Top_A4.A4, GTSCard.GetEnc(Top_A4.A4));
                    source = new CancellationTokenSource();
                    CancellationToken token = source.Token;
                    if (vpoint.Name.Contains("SuckPicker"))
                    {
                        AxisPointDisp _vpoint = vpoint; ;
                        using var db = new DTContext(partNum);
                        switch (SuckerSelectedIndex)
                        {
                            case 1:
                                {
                                    var _p0 = db.XYZRPoints.FirstOrDefault(p => p.Name == "CenterSuck2" && p.Floor == 2);
                                    if (_p0 != null)
                                    {
                                        _vpoint = new AxisPointDisp
                                        {
                                            ZIndex = 1,
                                            RIndex = 0,
                                            X = vpoint.X - _p0.X,
                                            Y = vpoint.Y - _p0.Y,
                                            Z = vpoint.Z,
                                            R = vpoint.R
                                        };
                                    }
                                }
                                break;
                            case 2:
                                {
                                    var _p0 = db.XYZRPoints.FirstOrDefault(p => p.Name == "CenterSuck3" && p.Floor == 2);
                                    if (_p0 != null)
                                    {
                                        _vpoint = new AxisPointDisp
                                        {
                                            ZIndex = 2,
                                            RIndex = 1,
                                            X = vpoint.X - _p0.X,
                                            Y = vpoint.Y - _p0.Y,
                                            Z = vpoint.Z,
                                            R = vpoint.R
                                        };
                                    }
                                }
                                break;
                            case 3:
                                {
                                    var _p0 = db.XYZRPoints.FirstOrDefault(p => p.Name == "CenterSuck4" && p.Floor == 2);
                                    if (_p0 != null)
                                    {
                                        _vpoint = new AxisPointDisp
                                        {
                                            ZIndex = 3,
                                            RIndex = 1,
                                            X = vpoint.X - _p0.X,
                                            Y = vpoint.Y - _p0.Y,
                                            Z = vpoint.Z,
                                            R = vpoint.R
                                        };
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                        await Task.Run(() => JumpXYZRAction(token, _vpoint), token).ContinueWith(t => { IsBusy = false; });
                    }
                    else
                    {
                        await Task.Run(() => JumpXYZRAction(token, vpoint), token).ContinueWith(t => { IsBusy = false; });
                    }
                }
            }
            else
            {
                _dialogHostService.MessageBox(MessageType.Message, "信息", "未选择任何项目");
            }
        }
        void ExecutePointList1TeachPosCommand(object obj)
        {
            if (obj != null)
            {
                var vpoint = (AxisPointDisp)obj;
                var result = _dialogHostService.MessageBox(MessageType.Confirm, "信息", $"确认示教\"{vpoint.Alias}\"吗？");
                if (result == ButtonResult.Yes)
                {
                    if (vpoint.Name.Contains("SuckPicker"))
                    {
                        using var db = new DTContext(partNum);
                        switch (SuckerSelectedIndex)
                        {
                            case 0:
                                vpoint.X = Math.Round(GTSCard.GetEnc(Bottom_A8.A1), 3);
                                vpoint.Y = Math.Round(GTSCard.GetEnc(Bottom_A8.A2), 3);
                                vpoint.Z = Math.Round(GTSCard.GetEnc(Bottom_A8.A4), 3);
                                vpoint.R = Math.Round(GTSCard.GetEnc(Top_A4.A3), 2);
                                break;
                            case 1:
                                {
                                    var _p0 = db.XYZRPoints.FirstOrDefault(p => p.Name == "CenterSuck2" && p.Floor == 2);
                                    if (_p0 != null)
                                    {
                                        vpoint.X = Math.Round(GTSCard.GetEnc(Bottom_A8.A1) + _p0.X, 3);
                                        vpoint.Y = Math.Round(GTSCard.GetEnc(Bottom_A8.A2) + _p0.Y, 3);
                                        vpoint.Z = Math.Round(GTSCard.GetEnc(Bottom_A8.A5), 3);
                                        vpoint.R = Math.Round(GTSCard.GetEnc(Top_A4.A3), 2);
                                    }
                                }
                                break;
                            case 2:
                                {
                                    var _p0 = db.XYZRPoints.FirstOrDefault(p => p.Name == "CenterSuck3" && p.Floor == 2);
                                    if (_p0 != null)
                                    {
                                        vpoint.X = Math.Round(GTSCard.GetEnc(Bottom_A8.A1) + _p0.X, 3);
                                        vpoint.Y = Math.Round(GTSCard.GetEnc(Bottom_A8.A2) + _p0.Y, 3);
                                        vpoint.Z = Math.Round(GTSCard.GetEnc(Bottom_A8.A6), 3);
                                        vpoint.R = Math.Round(GTSCard.GetEnc(Top_A4.A4), 2);
                                    }
                                }
                                break;
                            case 3:
                                {
                                    var _p0 = db.XYZRPoints.FirstOrDefault(p => p.Name == "CenterSuck4" && p.Floor == 2);
                                    if (_p0 != null)
                                    {
                                        vpoint.X = Math.Round(GTSCard.GetEnc(Bottom_A8.A1) + _p0.X, 3);
                                        vpoint.Y = Math.Round(GTSCard.GetEnc(Bottom_A8.A2) + _p0.Y, 3);
                                        vpoint.Z = Math.Round(GTSCard.GetEnc(Bottom_A8.A7), 3);
                                        vpoint.R = Math.Round(GTSCard.GetEnc(Top_A4.A4), 2);
                                    }
                                }
                                break;
                            default:
                                break;
                        }

                    }
                    else
                    {
                        vpoint.X = Math.Round(GTSCard.GetEnc(Bottom_A8.A1), 3);
                        vpoint.Y = Math.Round(GTSCard.GetEnc(Bottom_A8.A2), 3);
                        switch (vpoint.ZIndex)
                        {
                            case 0:
                                vpoint.Z = Math.Round(GTSCard.GetEnc(Bottom_A8.A4), 3);
                                break;
                            case 1:
                                vpoint.Z = Math.Round(GTSCard.GetEnc(Bottom_A8.A5), 3);
                                break;
                            case 2:
                                vpoint.Z = Math.Round(GTSCard.GetEnc(Bottom_A8.A6), 3);
                                break;
                            default:
                                vpoint.Z = Math.Round(GTSCard.GetEnc(Bottom_A8.A7), 3);
                                break;
                        }
                        switch (vpoint.RIndex)
                        {
                            case 0:
                                vpoint.R = Math.Round(GTSCard.GetEnc(Top_A4.A3), 2);
                                break;
                            default:
                                vpoint.R = Math.Round(GTSCard.GetEnc(Top_A4.A4), 2);
                                break;
                        }

                    }
                   
                    using (var db = new DTContext(partNum))
                    {
                        var point = db.XYZRPoints.FirstOrDefault(p => p.Id == vpoint.Id && p.Floor == 2);
                        if (point != null)
                        {
                            logger.Info($"{MLogType.调试}:点坐标\"{vpoint.Alias}\"({point.X},{point.Y},{point.Z},{point.R})→({vpoint.X},{vpoint.Y},{vpoint.Z},{vpoint.R})");
                            point.X = vpoint.X;
                            point.Y = vpoint.Y;
                            point.Z = vpoint.Z;
                            point.R = vpoint.R;
                            db.SaveChanges();
                        }
                    }
                }
            }
            else
            {
                _dialogHostService.MessageBox(MessageType.Message, "信息", "未选择任何项目");
            }
        }
        void ExecutePointList1RowEditEndingCommand(object obj)
        {
            if (obj != null)
            {
                var vpoint = (AxisPointDisp)obj;
                using (var db = new DTContext(partNum))
                {
                    var point = db.XYZRPoints.FirstOrDefault(p => p.Id == vpoint.Id && p.Floor == 2);
                    if (point != null)
                    {
                        logger.Info($"{MLogType.调试}:点坐标\"{vpoint.Alias}\"({point.X},{point.Y},{point.Z},{point.R})→({vpoint.X},{vpoint.Y},{vpoint.Z},{vpoint.R})");
                        point.X = vpoint.X;
                        point.Y = vpoint.Y;
                        point.Z = vpoint.Z;
                        point.R = vpoint.R;
                        db.SaveChanges();
                    }
                }
            }
        }
        void ExecuteJog_MouseUpCommand()
        {
            isJogButtonPressing = false;
            if (!isPrtMoving)
            {
                GTSCard.AxisStop(Bottom_A8.A1, 1);
                GTSCard.AxisStop(Bottom_A8.A2, 1);
                GTSCard.AxisStop(Bottom_A8.A4, 1);
                GTSCard.AxisStop(Bottom_A8.A5, 1);
                GTSCard.AxisStop(Bottom_A8.A6, 1);
                GTSCard.AxisStop(Bottom_A8.A7, 1);
                GTSCard.AxisStop(Top_A4.A3, 1);
                GTSCard.AxisStop(Top_A4.A4, 1);

                if (source1 != null)
                {
                    source1.Cancel();
                }
                IsBusy = false;
            }

        }
        void ExecuteJog_MouseDownCommand(object obj)
        {
            double xjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "XJogSpeed").Value.ToString());
            double yjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "YJogSpeed").Value.ToString());
            double zjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "ZJogSpeed").Value.ToString());
            double rjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "RJogSpeed").Value.ToString());
            int index = int.Parse(obj.ToString());
            int _index = (index - 1) / 2;
            int dir = index % 2 != 0 ? 1 : 0;
            isJogButtonPressing = true;
            if (!isPrtMoving && !IsBusy)
            {
                IsBusy = true;
                isPrtMoving = true;
                AxisParm _axis;
                double _speed;
                switch (_index)
                {
                    case 0://X
                        _axis = Bottom_A8.A1;
                        _speed = xjogspeed;
                        break;
                    case 1://Y
                        _axis = Bottom_A8.A2;
                        _speed = yjogspeed;
                        break;
                    case 2://Z1
                        _axis = Bottom_A8.A4;
                        _speed = zjogspeed;
                        break;
                    case 3://Z2
                        _axis = Bottom_A8.A5;
                        _speed = zjogspeed;
                        break;
                    case 4://Z3
                        _axis = Bottom_A8.A6;
                        _speed = zjogspeed;
                        break;
                    case 5://Z4
                        _axis = Bottom_A8.A7;
                        _speed = zjogspeed;
                        break;
                    case 6://R1
                        _axis = Top_A4.A3;
                        _speed = rjogspeed;
                        break;
                    case 7://R2
                    default:
                        _axis = Top_A4.A4;
                        _speed = rjogspeed;
                        break;
                }
                var curPos = GTSCard.GetPos(_axis);
                double _tarPos = 0;
                if (dir == 1)
                {
                    _tarPos = curPos + JogDist;
                }
                else
                {
                    _tarPos = curPos - JogDist;
                }
                source1 = new CancellationTokenSource();
                CancellationToken token = source1.Token;
                Task.Run(() => GoAction(token, _axis, _tarPos, _speed), token).ContinueWith(arg => {

                    isPrtMoving = false;
                    if (isJogButtonPressing)
                    {
                        GTSCard.AxisJog(_axis, (ushort)dir, _speed);
                    }
                    else
                    {
                        IsBusy = false;
                    }
                });
            }
            
        }
        void ExecuteServoOnCommand(object obj)
        {
            switch (obj.ToString())
            {
                case "1":
                    {
                        if (AxisStatusDisps[0].ServoOnState)
                        {
                            GTSCard.ServoOff(Bottom_A8.A1);
                        }
                        else
                        {
                            GTSCard.ServoOn(Bottom_A8.A1);
                            GTSCard.AxisPosSet(Bottom_A8.A1, GTSCard.GetEnc(Bottom_A8.A1));
                        }
                    }
                    break;
                case "2":
                    {
                        if (AxisStatusDisps[1].ServoOnState)
                        {
                            GTSCard.ServoOff(Bottom_A8.A2);
                        }
                        else
                        {
                            GTSCard.ServoOn(Bottom_A8.A2);
                            GTSCard.AxisPosSet(Bottom_A8.A2, GTSCard.GetEnc(Bottom_A8.A2));
                        }
                    }
                    break;
                case "3":
                    {
                        if (AxisStatusDisps[2].ServoOnState)
                        {
                            GTSCard.ServoOff(Bottom_A8.A4);
                            GTSCard.SetDo(Bottom_A8.A1.CardNo, 0, 1);
                        }
                        else
                        {
                            GTSCard.ServoOn(Bottom_A8.A4);
                            GTSCard.SetDo(Bottom_A8.A1.CardNo, 0, 0);
                            GTSCard.AxisPosSet(Bottom_A8.A4, GTSCard.GetEnc(Bottom_A8.A4));
                        }
                    }
                    break;
                case "4":
                    {
                        if (AxisStatusDisps[3].ServoOnState)
                        {
                            GTSCard.ServoOff(Bottom_A8.A5);
                            GTSCard.SetDo(Bottom_A8.A1.CardNo, 1, 1);
                        }
                        else
                        {
                            GTSCard.ServoOn(Bottom_A8.A5);
                            GTSCard.SetDo(Bottom_A8.A1.CardNo, 1, 0);
                            GTSCard.AxisPosSet(Bottom_A8.A5, GTSCard.GetEnc(Bottom_A8.A5));
                        }
                    }
                    break;
                case "5":
                    {
                        if (AxisStatusDisps[4].ServoOnState)
                        {
                            GTSCard.ServoOff(Bottom_A8.A6);
                            GTSCard.SetDo(Bottom_A8.A1.CardNo, 2, 1);
                        }
                        else
                        {
                            GTSCard.ServoOn(Bottom_A8.A6);
                            GTSCard.SetDo(Bottom_A8.A1.CardNo, 2, 0);
                            GTSCard.AxisPosSet(Bottom_A8.A6, GTSCard.GetEnc(Bottom_A8.A6));
                        }
                    }
                    break;
                case "6":
                    {
                        if (AxisStatusDisps[5].ServoOnState)
                        {
                            GTSCard.ServoOff(Bottom_A8.A7);
                            GTSCard.SetDo(Bottom_A8.A1.CardNo, 3, 1);
                        }
                        else
                        {
                            GTSCard.ServoOn(Bottom_A8.A7);
                            GTSCard.SetDo(Bottom_A8.A1.CardNo, 3, 0);
                            GTSCard.AxisPosSet(Bottom_A8.A7, GTSCard.GetEnc(Bottom_A8.A7));
                        }
                    }
                    break;
                case "7":
                    {
                        if (AxisStatusDisps[6].ServoOnState)
                        {
                            GTSCard.ServoOff(Top_A4.A3);
                        }
                        else
                        {
                            GTSCard.ServoOn(Top_A4.A3);
                            GTSCard.AxisPosSet(Top_A4.A3, GTSCard.GetEnc(Top_A4.A3));
                        }
                    }
                    break;
                case "8":
                    {
                        if (AxisStatusDisps[7].ServoOnState)
                        {
                            GTSCard.ServoOff(Top_A4.A4);
                        }
                        else
                        {
                            GTSCard.ServoOn(Top_A4.A4);
                            GTSCard.AxisPosSet(Top_A4.A4, GTSCard.GetEnc(Top_A4.A4));
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        void ExecuteClearAlarmCommand(object obj)
        {
            switch (obj.ToString())
            {
                case "1":
                    GTSCard.ClearAlm(Bottom_A8.A1);
                    break;
                case "2":
                    GTSCard.ClearAlm(Bottom_A8.A2);
                    break;
                case "3":
                    GTSCard.ClearAlm(Bottom_A8.A4);
                    break;
                case "4":
                    GTSCard.ClearAlm(Bottom_A8.A5);
                    break;
                case "5":
                    GTSCard.ClearAlm(Bottom_A8.A6);
                    break;
                case "6":
                    GTSCard.ClearAlm(Bottom_A8.A7);
                    break;
                case "7":
                    GTSCard.ClearAlm(Top_A4.A3);
                    break;
                case "8":
                    GTSCard.ClearAlm(Top_A4.A4);
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 导航
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            partNum = parameters.GetValue<string>("PartNum");
            using (var db = new DTContext(partNum))
            {
                {
                    var points = db.XYZRPoints.Where(p => (p.Name == "Init" || p.Name == "SuckPicker1" || p.Name == "SuckPicker2" || p.Name == "SuckPicker3" || p.Name == "SuckPicker4"
                    || p.Name == "FlyStartPicker" || p.Name == "FlyEndPicker" || p.Name == "BottomCameraCalib_Sucker1" || p.Name == "BottomCameraCalib_Sucker2" || p.Name == "BottomCameraCalib_Sucker3" || p.Name == "BottomCameraCalib_Sucker4"
                    || p.Name == "BottomCameraCalib_Sucker1" || p.Name == "BottomCameraCalib_Sucker2" || p.Name == "BottomCameraCalib_Sucker3" || p.Name == "BottomCameraCalib_Sucker4"
                    || p.Name == "TopCameraCalib" || p.Name == "TopCameraCenter_Suck1" || p.Name == "BottomCameraFail_Sucker1"
                    || p.Name == "TopCameraBoardOrgGrab" || p.Name == "ButtomCameraGrab_Sucker1" || p.Name == "TopCameraMark1" || p.Name == "TopCameraMark2" || p.Name == "TestPastePoint") && p.Floor == 2);
                    if (points.Any())
                    {
                        var posList = points.ToList();
                        foreach (var item in posList)
                        {
                            PointList1.Add(
                                new AxisPointDisp
                                {
                                    Id = item.Id,
                                    Name = item.Name,
                                    Alias = item.Alias,
                                    X = item.X,
                                    Y = item.Y,
                                    Z = item.Z,
                                    R = item.R,
                                    ZIndex = item.ZIndex,
                                    RIndex = item.RIndex
                                }
                                );
                        }
                    }
                    var initp = PointList1.FirstOrDefault(p => p.Name == "Init");
                    if (initp != null)
                    {
                        TestPoint_X = initp.X;
                        TestPoint_Y = initp.Y;
                        TestPoint_Z1 = initp.Z;
                        TestPoint_Z2 = initp.Z;
                        TestPoint_Z3 = initp.Z;
                        TestPoint_Z4 = initp.Z;
                        TestPoint_R1 = initp.R;
                        TestPoint_R2 = initp.R;
                    }
                }
                {
                    var zsfs = db.MParams.Where(p => (p.Name == "Z1SafePos" || p.Name == "Z2SafePos" || p.Name == "Z3SafePos" || p.Name == "Z4SafePos") && p.Floor == 2);
                    if (zsfs.Any())
                    {
                        var zsfList = zsfs.ToList();
                        foreach (var item in zsfList)
                        {
                            PointList2.Add(new AxisZSafeDisp
                            {
                                Id = item.Id,
                                Alias = item.Alias,
                                Name = item.Name,
                                ZPos = double.Parse(item.Value),
                                Unit = item.Unit
                            });
                        }
                    }
                }
                {
                    var parms = db.MParams.Where(p => (p.Name == "XJogSpeed" || p.Name == "YJogSpeed" || p.Name == "ZJogSpeed" || p.Name == "RJogSpeed"
                    || p.Name == "XRunSpeed" || p.Name == "YRunSpeed" || p.Name == "ZRunSpeed" || p.Name == "RRunSpeed"
                    || p.Name == "ZSlowDistSuck" || p.Name == "ZSlowSpeedSuck" || p.Name == "ZSlowDistPaste" || p.Name == "ZSlowSpeedPaste") && p.Floor == 2);
                    if (parms.Any())
                    {
                        var parmList = parms.ToList();
                        foreach (var item in parmList)
                        {
                            ParamList1.Add(new ParamDisp
                            {
                                Id = item.Id,
                                Alias = item.Alias,
                                Name = item.Name,
                                Value = double.Parse(item.Value),
                                Unit = item.Unit
                            });
                        }
                    }
                }
            }
            using (var sysDb = new SysContext())
            {
                var abspos = sysDb.MSystemParams.FirstOrDefault(p => p.Name == "BottomZ1");
                if (abspos != null)
                {
                    PointList3.Add(new AxisZSafeDisp
                    {
                        Id = abspos.Id,
                        Alias = "Z1绝对值坐标",
                        Name = abspos.Name,
                        ZPos = double.Parse(abspos.Value),
                        Unit = "mm"
                    });
                }
                abspos = sysDb.MSystemParams.FirstOrDefault(p => p.Name == "BottomZ2");
                if (abspos != null)
                {
                    PointList3.Add(new AxisZSafeDisp
                    {
                        Id = abspos.Id,
                        Alias = "Z2绝对值坐标",
                        Name = abspos.Name,
                        ZPos = double.Parse(abspos.Value),
                        Unit = "mm"
                    });
                }
                abspos = sysDb.MSystemParams.FirstOrDefault(p => p.Name == "BottomZ3");
                if (abspos != null)
                {
                    PointList3.Add(new AxisZSafeDisp
                    {
                        Id = abspos.Id,
                        Alias = "Z3绝对值坐标",
                        Name = abspos.Name,
                        ZPos = double.Parse(abspos.Value),
                        Unit = "mm"
                    });
                }
                abspos = sysDb.MSystemParams.FirstOrDefault(p => p.Name == "BottomZ4");
                if (abspos != null)
                {
                    PointList3.Add(new AxisZSafeDisp
                    {
                        Id = abspos.Id,
                        Alias = "Z4绝对值坐标",
                        Name = abspos.Name,
                        ZPos = double.Parse(abspos.Value),
                        Unit = "mm"
                    });
                }
                abspos = sysDb.MSystemParams.FirstOrDefault(p => p.Name == "BottomR1");
                if (abspos != null)
                {
                    PointList3.Add(new AxisZSafeDisp
                    {
                        Id = abspos.Id,
                        Alias = "R1绝对值坐标",
                        Name = abspos.Name,
                        ZPos = double.Parse(abspos.Value),
                        Unit = "°"
                    });
                }
                abspos = sysDb.MSystemParams.FirstOrDefault(p => p.Name == "BottomR2");
                if (abspos != null)
                {
                    PointList3.Add(new AxisZSafeDisp
                    {
                        Id = abspos.Id,
                        Alias = "R2绝对值坐标",
                        Name = abspos.Name,
                        ZPos = double.Parse(abspos.Value),
                        Unit = "°"
                    });
                }
            }
            source0 = new CancellationTokenSource();
            CancellationToken token = source0.Token;
            Task.Run(() => UpdateAction(token), token);
        }
        public override void OnDialogClosed()
        {
            if (source != null)
            {
                source.Cancel();
            }
            if (source0 != null)
            {
                source0.Cancel();
            }
            if (source1 != null)
            {
                source1.Cancel();
            }
        }
        #endregion
        public BottomFloorAxisDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            Title = "下层轴";
            SuckerSelectedIndex = 0;
            JogDist = 0.1;
            AxisStatusDisps = new ObservableCollection<AxisStatusDisp>();
            for (int i = 0; i < 8; i++)
            {
                AxisStatusDisps.Add(new AxisStatusDisp());
            }
            PointList1 = new ObservableCollection<AxisPointDisp>();
            PointList2 = new ObservableCollection<AxisZSafeDisp>();
            PointList3 = new ObservableCollection<AxisZSafeDisp>();
            ParamList1 = new ObservableCollection<ParamDisp>();
            IsBusy = false;
            EXI = new ObservableCollection<bool>();
            for (int i = 0; i < 16; i++)
            {
                EXI.Add(false);
            }
            EXO = new ObservableCollection<bool>();
            for (int i = 0; i < 16; i++)
            {
                EXO.Add(false);
            }
            Top_A4 = containerProvider.Resolve<IGTSCardService>("Top_A4");
            Bottom_A8 = containerProvider.Resolve<IGTSCardService>("Bottom_A8");
            servoModbus = containerProvider.Resolve<IServoModbusService>("ServoModbus");
        }
        #region 功能
        private void JumpXYZRAction(CancellationToken token, AxisPointDisp point)
        {
            try
            {
                double z1safe = PointList2.FirstOrDefault(p => p.Name == "Z1SafePos").ZPos;
                double z2safe = PointList2.FirstOrDefault(p => p.Name == "Z2SafePos").ZPos;
                double z3safe = PointList2.FirstOrDefault(p => p.Name == "Z3SafePos").ZPos;
                double z4safe = PointList2.FirstOrDefault(p => p.Name == "Z4SafePos").ZPos;
                double xjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "XJogSpeed").Value.ToString());
                double yjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "YJogSpeed").Value.ToString());
                double zjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "ZJogSpeed").Value.ToString());
                double rjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "RJogSpeed").Value.ToString());
                var axisX = Bottom_A8.A1;
                var axisY = Bottom_A8.A2;
                var axisZ1 = Bottom_A8.A4;
                var axisZ2 = Bottom_A8.A5;
                var axisZ3 = Bottom_A8.A6;
                var axisZ4 = Bottom_A8.A7;
                AxisParm axisZ, axisR;
                switch (point.ZIndex)
                {
                    case 0:
                        axisZ = Bottom_A8.A4;
                        break;
                    case 1:
                        axisZ = Bottom_A8.A5;
                        break;
                    case 2:
                        axisZ = Bottom_A8.A6;
                        break;
                    case 3:
                    default:
                        axisZ = Bottom_A8.A7;
                        break;
                }
                switch (point.RIndex)
                {
                    case 0:
                        axisR = Top_A4.A3;
                        break;
                    case 1:
                    default:
                        axisR = Top_A4.A4;
                        break;
                }
                int stepnum = 0;
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    switch (stepnum)
                    {
                        case 0://Z运动到安全位
                            {
                                GTSCard.AxisPosMove(ref axisZ1, z1safe, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ2, z2safe, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ3, z3safe, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ4, z4safe, zjogspeed);
                                stepnum = 1;
                            }
                            break;
                        case 1:
                            if (GTSCard.AxisPosMoveCheckDone(axisZ1) && GTSCard.AxisPosMoveCheckDone(axisZ2) && GTSCard.AxisPosMoveCheckDone(axisZ3) && GTSCard.AxisPosMoveCheckDone(axisZ4))
                            {
                                stepnum = 2;
                            }
                            break;
                        case 2:
                            {
                                GTSCard.AxisPosMove(ref axisX, point.X, xjogspeed);
                                GTSCard.AxisPosMove(ref axisY, point.Y, yjogspeed);
                                GTSCard.AxisPosMove(ref axisR, point.R, rjogspeed);
                                stepnum = 3;
                            }
                            break;
                        case 3:
                            if (GTSCard.AxisPosMoveCheckDone(axisX) && GTSCard.AxisPosMoveCheckDone(axisY) && GTSCard.AxisPosMoveCheckDone(axisR))
                            {
                                stepnum = 4;
                            }
                            break;
                        case 4:
                            {
                                GTSCard.AxisPosMove(ref axisZ, point.Z, zjogspeed);
                                stepnum = 5;
                            }
                            break;
                        case 5:
                            if (GTSCard.AxisPosMoveCheckDone(axisZ))
                            {
                                return;
                            }
                            break;
                        default:
                            break;
                    }
                    System.Threading.Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Debug.WriteLine(ex.Message);
            }

        }
        private void GoXYZRAction(CancellationToken token, AxisPointDisp point)
        {
            try
            {
                double xjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "XJogSpeed").Value.ToString());
                double yjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "YJogSpeed").Value.ToString());
                double zjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "ZJogSpeed").Value.ToString());
                double rjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "RJogSpeed").Value.ToString());
                var axisX = Bottom_A8.A1;
                var axisY = Bottom_A8.A2;
                AxisParm axisZ, axisR;
                switch (point.ZIndex)
                {
                    case 0:
                        axisZ = Bottom_A8.A4;
                        break;
                    case 1:
                        axisZ = Bottom_A8.A5;
                        break;
                    case 2:
                        axisZ = Bottom_A8.A6;
                        break;
                    case 3:
                    default:
                        axisZ = Bottom_A8.A7;
                        break;
                }
                switch (point.RIndex)
                {
                    case 0:
                        axisR = Top_A4.A3;
                        break;
                    case 1:
                    default:
                        axisR = Top_A4.A4;
                        break;
                }
                int stepnum = 0;
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    switch (stepnum)
                    {
                        case 0:
                            {
                                GTSCard.AxisPosMove(ref axisX, point.X, xjogspeed);
                                GTSCard.AxisPosMove(ref axisY, point.Y, yjogspeed);
                                GTSCard.AxisPosMove(ref axisZ, point.Z, zjogspeed);
                                GTSCard.AxisPosMove(ref axisR, point.R, rjogspeed);
                                stepnum = 1;
                            }
                            break;
                        case 1:
                            if (GTSCard.AxisPosMoveCheckDone(axisX) && GTSCard.AxisPosMoveCheckDone(axisY) && GTSCard.AxisPosMoveCheckDone(axisZ) && GTSCard.AxisPosMoveCheckDone(axisR))
                            {
                                return;
                            }
                            break;
                        default:
                            break;
                    }
                    System.Threading.Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Debug.WriteLine(ex.Message);
            }

        }
        private void GoAction(CancellationToken token, AxisZSafeDisp point)
        {
            try
            {
                double zjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "ZJogSpeed").Value.ToString());
                AxisParm axisZ;
                switch (point.Name)
                {
                    case "Z1SafePos":
                        axisZ = Bottom_A8.A4;
                        break;
                    case "Z2SafePos":
                        axisZ = Bottom_A8.A5;
                        break;
                    case "Z3SafePos":
                        axisZ = Bottom_A8.A6;
                        break;
                    case "Z4SafePos":
                    default:
                        axisZ = Bottom_A8.A7;
                        break;
                }
                int stepnum = 0;
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    switch (stepnum)
                    {
                        case 0:
                            {
                                GTSCard.AxisPosMove(ref axisZ, point.ZPos, zjogspeed);
                                stepnum = 1;
                            }
                            break;
                        case 1:
                            if (GTSCard.AxisPosMoveCheckDone(axisZ))
                            {
                                return;
                            }
                            break;
                        default:
                            break;
                    }
                    System.Threading.Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Debug.WriteLine(ex.Message);
            }
        }
        private void GoAction(CancellationToken token, AxisParm axisN, double tartgetN, double speed)
        {
            try
            {
                int stepnum = 0;
                Stopwatch sw = Stopwatch.StartNew();
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    switch (stepnum)
                    {
                        case 0:
                            {
                                GTSCard.AxisPosMove(ref axisN, tartgetN, speed);
                                stepnum = 1;
                            }
                            break;
                        case 1:
                            if (GTSCard.AxisPosMoveCheckDone(axisN))
                            {
                                stepnum = 2;
                            }
                            break;
                        case 2:
                            if (sw.Elapsed.TotalSeconds > 0.5)
                            {
                                sw.Stop();
                                return;
                            }
                            break;
                        default:
                            break;
                    }
                    System.Threading.Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Debug.WriteLine(ex.Message);
            }
        }
        private void UpdateAction(CancellationToken token)
        {
            while (true)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }
                try
                {
                    //X轴
                    AxisStatus astatus = GTSCard.GetAxisStatus(Bottom_A8.A1);
                    AxisStatusDisps[0].AlmState = astatus.FlagAlm;
                    AxisStatusDisps[0].HomeState = astatus.FlagHome;
                    AxisStatusDisps[0].PosLimitState = astatus.FlagPosLimit;
                    AxisStatusDisps[0].NeglimitState = astatus.FlagNeglimit;
                    AxisStatusDisps[0].ServoOnState = astatus.FlagServoOn;
                    AxisStatusDisps[0].MoveState = !astatus.FlagMoveEnd;
                    AxisStatusDisps[0].PosPrf = Math.Round(astatus.PrfPos, 3);
                    AxisStatusDisps[0].PosEnc = Math.Round(astatus.EncPos, 3);
                    //Y轴
                    astatus = GTSCard.GetAxisStatus(Bottom_A8.A2);
                    AxisStatusDisps[1].AlmState = astatus.FlagAlm;
                    AxisStatusDisps[1].HomeState = astatus.FlagHome;
                    AxisStatusDisps[1].PosLimitState = astatus.FlagPosLimit;
                    AxisStatusDisps[1].NeglimitState = astatus.FlagNeglimit;
                    AxisStatusDisps[1].ServoOnState = astatus.FlagServoOn;
                    AxisStatusDisps[1].MoveState = !astatus.FlagMoveEnd;
                    AxisStatusDisps[1].PosPrf = Math.Round(astatus.PrfPos, 3);
                    AxisStatusDisps[1].PosEnc = Math.Round(astatus.EncPos, 3);
                    //Z1轴
                    astatus = GTSCard.GetAxisStatus(Bottom_A8.A4);
                    AxisStatusDisps[2].AlmState = astatus.FlagAlm;
                    AxisStatusDisps[2].HomeState = astatus.FlagHome;
                    AxisStatusDisps[2].PosLimitState = astatus.FlagPosLimit;
                    AxisStatusDisps[2].NeglimitState = astatus.FlagNeglimit;
                    AxisStatusDisps[2].ServoOnState = astatus.FlagServoOn;
                    AxisStatusDisps[2].MoveState = !astatus.FlagMoveEnd;
                    AxisStatusDisps[2].PosPrf = Math.Round(astatus.PrfPos, 3);
                    AxisStatusDisps[2].PosEnc = Math.Round(astatus.EncPos, 3);
                    //Z2轴
                    astatus = GTSCard.GetAxisStatus(Bottom_A8.A5);
                    AxisStatusDisps[3].AlmState = astatus.FlagAlm;
                    AxisStatusDisps[3].HomeState = astatus.FlagHome;
                    AxisStatusDisps[3].PosLimitState = astatus.FlagPosLimit;
                    AxisStatusDisps[3].NeglimitState = astatus.FlagNeglimit;
                    AxisStatusDisps[3].ServoOnState = astatus.FlagServoOn;
                    AxisStatusDisps[3].MoveState = !astatus.FlagMoveEnd;
                    AxisStatusDisps[3].PosPrf = Math.Round(astatus.PrfPos, 3);
                    AxisStatusDisps[3].PosEnc = Math.Round(astatus.EncPos, 3);
                    //Z3轴
                    astatus = GTSCard.GetAxisStatus(Bottom_A8.A6);
                    AxisStatusDisps[4].AlmState = astatus.FlagAlm;
                    AxisStatusDisps[4].HomeState = astatus.FlagHome;
                    AxisStatusDisps[4].PosLimitState = astatus.FlagPosLimit;
                    AxisStatusDisps[4].NeglimitState = astatus.FlagNeglimit;
                    AxisStatusDisps[4].ServoOnState = astatus.FlagServoOn;
                    AxisStatusDisps[4].MoveState = !astatus.FlagMoveEnd;
                    AxisStatusDisps[4].PosPrf = Math.Round(astatus.PrfPos, 3);
                    AxisStatusDisps[4].PosEnc = Math.Round(astatus.EncPos, 3);
                    //Z4轴
                    astatus = GTSCard.GetAxisStatus(Bottom_A8.A7);
                    AxisStatusDisps[5].AlmState = astatus.FlagAlm;
                    AxisStatusDisps[5].HomeState = astatus.FlagHome;
                    AxisStatusDisps[5].PosLimitState = astatus.FlagPosLimit;
                    AxisStatusDisps[5].NeglimitState = astatus.FlagNeglimit;
                    AxisStatusDisps[5].ServoOnState = astatus.FlagServoOn;
                    AxisStatusDisps[5].MoveState = !astatus.FlagMoveEnd;
                    AxisStatusDisps[5].PosPrf = Math.Round(astatus.PrfPos, 3);
                    AxisStatusDisps[5].PosEnc = Math.Round(astatus.EncPos, 3);
                    //R1轴
                    astatus = GTSCard.GetAxisStatus(Top_A4.A3);
                    AxisStatusDisps[6].AlmState = astatus.FlagAlm;
                    AxisStatusDisps[6].HomeState = astatus.FlagHome;
                    AxisStatusDisps[6].PosLimitState = astatus.FlagPosLimit;
                    AxisStatusDisps[6].NeglimitState = astatus.FlagNeglimit;
                    AxisStatusDisps[6].ServoOnState = astatus.FlagServoOn;
                    AxisStatusDisps[6].MoveState = !astatus.FlagMoveEnd;
                    AxisStatusDisps[6].PosPrf = Math.Round(astatus.PrfPos, 3);
                    AxisStatusDisps[6].PosEnc = Math.Round(astatus.EncPos, 3);
                    //R2轴
                    astatus = GTSCard.GetAxisStatus(Top_A4.A4);
                    AxisStatusDisps[7].AlmState = astatus.FlagAlm;
                    AxisStatusDisps[7].HomeState = astatus.FlagHome;
                    AxisStatusDisps[7].PosLimitState = astatus.FlagPosLimit;
                    AxisStatusDisps[7].NeglimitState = astatus.FlagNeglimit;
                    AxisStatusDisps[7].ServoOnState = astatus.FlagServoOn;
                    AxisStatusDisps[7].MoveState = !astatus.FlagMoveEnd;
                    AxisStatusDisps[7].PosPrf = Math.Round(astatus.PrfPos, 3);
                    AxisStatusDisps[7].PosEnc = Math.Round(astatus.EncPos, 3);
                    //IO
                    int diport1 = GTSCard.GetDiPort1(Bottom_A8.A1.CardNo);
                    int doPort1 = GTSCard.GetDoPort1(Bottom_A8.A1.CardNo);
                    for (int i = 0; i < 16; i++)
                    {
                        EXI[i] = (diport1 & (1 << i)) == 0;
                        EXO[i] = (doPort1 & (1 << i)) == 0;
                    }
                }
                catch { }
                System.Threading.Thread.Sleep(200);
            }
        }
        private void JumpTestPointAction(CancellationToken token)
        {
            try
            {
                double z1safe = PointList2.FirstOrDefault(p => p.Name == "Z1SafePos").ZPos;
                double z2safe = PointList2.FirstOrDefault(p => p.Name == "Z2SafePos").ZPos;
                double z3safe = PointList2.FirstOrDefault(p => p.Name == "Z3SafePos").ZPos;
                double z4safe = PointList2.FirstOrDefault(p => p.Name == "Z4SafePos").ZPos;
                double xjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "XJogSpeed").Value.ToString());
                double yjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "YJogSpeed").Value.ToString());
                double zjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "ZJogSpeed").Value.ToString());
                double rjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "RJogSpeed").Value.ToString());
                var axisX = Bottom_A8.A1;
                var axisY = Bottom_A8.A2;
                var axisZ1 = Bottom_A8.A4;
                var axisZ2 = Bottom_A8.A5;
                var axisZ3 = Bottom_A8.A6;
                var axisZ4 = Bottom_A8.A7;
                var axisR1 = Top_A4.A3;
                var axisR2 = Top_A4.A4;
                
                int stepnum = 0;
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    switch (stepnum)
                    {
                        case 0://Z运动到安全位
                            {
                                GTSCard.AxisPosMove(ref axisZ1, z1safe, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ2, z2safe, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ3, z3safe, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ4, z4safe, zjogspeed);
                                stepnum = 1;
                            }
                            break;
                        case 1:
                            if (GTSCard.AxisPosMoveCheckDone(axisZ1) && GTSCard.AxisPosMoveCheckDone(axisZ2) && GTSCard.AxisPosMoveCheckDone(axisZ3) && GTSCard.AxisPosMoveCheckDone(axisZ4))
                            {
                                stepnum = 2;
                            }
                            break;
                        case 2:
                            {
                                GTSCard.AxisPosMove(ref axisX, TestPoint_X, xjogspeed);
                                GTSCard.AxisPosMove(ref axisY, TestPoint_Y, yjogspeed);
                                GTSCard.AxisPosMove(ref axisR1, TestPoint_R1, rjogspeed);
                                GTSCard.AxisPosMove(ref axisR2, TestPoint_R2, rjogspeed);
                                stepnum = 3;
                            }
                            break;
                        case 3:
                            if (GTSCard.AxisPosMoveCheckDone(axisX) && GTSCard.AxisPosMoveCheckDone(axisY) && GTSCard.AxisPosMoveCheckDone(axisR1) && GTSCard.AxisPosMoveCheckDone(axisR2))
                            {
                                stepnum = 4;
                            }
                            break;
                        case 4:
                            {
                                GTSCard.AxisPosMove(ref axisZ1, TestPoint_Z1, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ2, TestPoint_Z2, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ3, TestPoint_Z3, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ4, TestPoint_Z4, zjogspeed);
                                stepnum = 5;
                            }
                            break;
                        case 5:
                            if (GTSCard.AxisPosMoveCheckDone(axisZ1) && GTSCard.AxisPosMoveCheckDone(axisZ2) && GTSCard.AxisPosMoveCheckDone(axisZ3) && GTSCard.AxisPosMoveCheckDone(axisZ4))
                            {
                                return;
                            }
                            break;
                        default:
                            break;
                    }
                    System.Threading.Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Debug.WriteLine(ex.Message);
            }
        }
        private void GoTestPointAction(CancellationToken token)
        {
            try
            {
                double xjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "XJogSpeed").Value.ToString());
                double yjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "YJogSpeed").Value.ToString());
                double zjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "ZJogSpeed").Value.ToString());
                double rjogspeed = double.Parse(ParamList1.FirstOrDefault(p => p.Name == "RJogSpeed").Value.ToString());
                var axisX = Bottom_A8.A1;
                var axisY = Bottom_A8.A2;
                var axisZ1 = Bottom_A8.A4;
                var axisZ2 = Bottom_A8.A5;
                var axisZ3 = Bottom_A8.A6;
                var axisZ4 = Bottom_A8.A7;
                var axisR1 = Top_A4.A3;
                var axisR2 = Top_A4.A4;
                int stepnum = 0;
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    switch (stepnum)
                    {
                        case 0:
                            {
                                GTSCard.AxisPosMove(ref axisX, TestPoint_X, xjogspeed);
                                GTSCard.AxisPosMove(ref axisY, TestPoint_Y, yjogspeed);
                                GTSCard.AxisPosMove(ref axisZ1, TestPoint_Z1, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ2, TestPoint_Z2, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ3, TestPoint_Z3, zjogspeed);
                                GTSCard.AxisPosMove(ref axisZ4, TestPoint_Z4, zjogspeed);
                                GTSCard.AxisPosMove(ref axisR1, TestPoint_R1, rjogspeed);
                                GTSCard.AxisPosMove(ref axisR2, TestPoint_R2, rjogspeed);
                                stepnum = 1;
                            }
                            break;
                        case 1:
                            if (GTSCard.AxisPosMoveCheckDone(axisX) && GTSCard.AxisPosMoveCheckDone(axisY) && 
                                GTSCard.AxisPosMoveCheckDone(axisZ1) && GTSCard.AxisPosMoveCheckDone(axisZ2) && GTSCard.AxisPosMoveCheckDone(axisZ3) && GTSCard.AxisPosMoveCheckDone(axisZ4) &&
                                GTSCard.AxisPosMoveCheckDone(axisR1) && GTSCard.AxisPosMoveCheckDone(axisR2))
                            {
                                return;
                            }
                            break;
                        default:
                            break;
                    }
                    System.Threading.Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Debug.WriteLine(ex.Message);
            }

        }
        #endregion
    }
}
